﻿"use strict";

angular.module('ecApp').directive('ecAppDashboard', ['$localStorage', 'dataService', '$window',
    function ($localStorage, dataService, $window) {
        return {
            scope: {
            },
            templateUrl: '/app/widgets/ecAppDashboardDirective.html',
            link: function (scope, el, attr) {

                scope.onBuildClick = function () {
                    dataService.requestBuild(scope.widgets).then(function (response) {
                        $window.open(response.data, '_blank');
                    });
                };

                scope.addNewWidget = function (widget) {
                    var newWidget = angular.copy(widget.settings);
                    scope.widgets.push(newWidget);
                }

                scope.title = 'Dashboard';

                scope.gristerOpts = {
                    columns: 12,
                    margins: [20, 20],
                    outerMargin: false,
                    pushing: true,
                    floating: true,
                    swapping: false
                };

                scope.widgetDefinitions = [
                    {
                        title: 'Project',
                        settings: {
                            sizeX: 3,
                            sizeY: 3,
                            minSizeX: 2,
                            minSizeY: 2,
                            template: '<ec-add-project></ec-add-project>',
                            widgetSetting: {
                            }
                        }
                    }
                ];

                scope.widgets = $localStorage.widgets || []

                scope.$watch('widgets', function () {
                    $localStorage.widgets = scope.widgets;
                },true);
            }
        };
    }]);